package xyz.thoughtset.viewer.modules.ds.jdbc.core.linker;


import com.zaxxer.hikari.HikariConfig;
import com.zaxxer.hikari.HikariDataSource;
import xyz.thoughtset.viewer.common.connector.entity.bo.ConnectorMeta;
import xyz.thoughtset.viewer.common.connector.entity.bo.Linker;
import xyz.thoughtset.viewer.common.connector.linker.ConnBuilder;

import javax.sql.DataSource;
import java.util.Properties;

public class JdbcConnBuilder extends ConnBuilder {

    public JdbcConnBuilder(Linker dataLinker) {
        super(dataLinker);
    }

    public Object buildConnect(ConnectorMeta connectorMeta, Properties properties){
        return createDataSource(buildConfig(connectorMeta, properties));
    }

    protected HikariConfig buildConfig(ConnectorMeta connectorMeta, Properties properties){
        HikariConfig config;
        if (properties!=null){
            config = new HikariConfig(properties);
        }else{
            config = new HikariConfig();
        }
        config.setConnectionTestQuery(connectorMeta.getTestQuery());
        config.setJdbcUrl(connectorMeta.getUrl());
        config.setUsername(connectorMeta.getAk());
        config.setPassword(connectorMeta.getSk());
        config.setDriverClassName(this.dataLinker.getDriverClassName());
        return config;
    }

    protected DataSource createDataSource(HikariConfig config){
        return new HikariDataSource(config);
    }

}
